package io.renren.modules.quickService.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.quickService.dao.UserExtraDao;
import io.renren.modules.quickService.entity.UserExtraEntity;
import io.renren.modules.quickService.service.UserExtraService;
import io.renren.modules.quickService.vo.OrderTakerVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("userExtraService")
public class UserExtraServiceImpl extends ServiceImpl<UserExtraDao, UserExtraEntity> implements UserExtraService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<UserExtraEntity> page = this.selectPage(
                new Query<UserExtraEntity>(params).getPage(),
                new EntityWrapper<UserExtraEntity>()
                        .eq(isNotBlank(params.get("name")), "extra_name", params.get("name"))
                        .eq(isNotBlank(params.get("phone")), "mobile", params.get("phone"))
                        .eq(isNotBlank(params.get("adminCode")), "admin_code", params.get("adminCode"))
        );

        return new PageUtils(page);
    }

    private boolean isNotBlank(Object object) {
        if (object != null) {
            return StringUtils.isNotBlank(object.toString());
        }
        return false;
    }

    @Override
    public UserExtraEntity queryUserExtraByMobile(String mobile) {
        UserExtraEntity userExtraEntity = new UserExtraEntity();
        userExtraEntity.setMobile(mobile);
        return baseMapper.selectOne(userExtraEntity);
    }

    @Override
    public UserExtraEntity queryUserExtraByID(final String ID) {
        UserExtraEntity userExtraEntity = new UserExtraEntity();
        userExtraEntity.setId(ID);
        return baseMapper.selectOne(userExtraEntity);
    }

    @Override
    public void audit(UserExtraEntity userExtraEntity) {
        updateById(userExtraEntity);
    }

    @Override
    public List<OrderTakerVO> takerList(String adminCode) {
        return baseMapper.takerList(adminCode);
    }
}
